Skip to content

feat: add sequential batch support #5762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

vinistevam
Copy link
Contributor

@vinistevam vinistevam commented May 6, 2025

Explanation

The TransactionController currently lacks support for sequential batch transactions, which are required for the stablecoin lending feature. Specifically, there is no mechanism to execute multiple transactions (e.g., approval + token deposit) sequentially while ensuring confirmation for each transaction. This limitation prevents efficient batch processing to support new features.

What solution do these changes offer?
The SequentialPublishBatchHook introduces a default mechanism for handling batch transactions when no custom publishBatchHook is provided. It ensures transactions are published sequentially, waiting for confirmation before proceeding to the next. If any transaction fails to publish or confirm, the batch process halts and throws an error.

Key Features:

  • Sequential Execution: Publishes transactions one at a time and waits for confirmation.
  • Error Handling: Halts the batch if any transaction fails to publish or confirm.
  • Polling for Confirmation: Retries confirmation checks up to a maximum number of attempts.

References

Fixes https://github.com/MetaMask/MetaMask-planning/issues/4695

Changelog

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed, highlighting breaking changes as necessary
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

@vinistevam vinistevam force-pushed the feat/add-sequential-batch-hook branch from 0e732db to 8b9e6f7 Compare May 6, 2025 08:17
@vinistevam
Copy link
Contributor Author

@metamaskbot publish-preview

Copy link
Contributor

github-actions bot commented May 6, 2025

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "27.0.0-preview-8b9e6f7b",
  "@metamask-previews/address-book-controller": "6.0.3-preview-8b9e6f7b",
  "@metamask-previews/announcement-controller": "7.0.3-preview-8b9e6f7b",
  "@metamask-previews/app-metadata-controller": "1.0.0-preview-8b9e6f7b",
  "@metamask-previews/approval-controller": "7.1.3-preview-8b9e6f7b",
  "@metamask-previews/assets-controllers": "60.0.0-preview-8b9e6f7b",
  "@metamask-previews/base-controller": "8.0.1-preview-8b9e6f7b",
  "@metamask-previews/bridge-controller": "20.0.0-preview-8b9e6f7b",
  "@metamask-previews/bridge-status-controller": "17.0.0-preview-8b9e6f7b",
  "@metamask-previews/build-utils": "3.0.3-preview-8b9e6f7b",
  "@metamask-previews/chain-agnostic-permission": "0.5.0-preview-8b9e6f7b",
  "@metamask-previews/composable-controller": "11.0.0-preview-8b9e6f7b",
  "@metamask-previews/controller-utils": "11.7.0-preview-8b9e6f7b",
  "@metamask-previews/delegation-controller": "0.1.0-preview-8b9e6f7b",
  "@metamask-previews/earn-controller": "0.12.0-preview-8b9e6f7b",
  "@metamask-previews/eip1193-permission-middleware": "0.1.0-preview-8b9e6f7b",
  "@metamask-previews/ens-controller": "16.0.0-preview-8b9e6f7b",
  "@metamask-previews/eth-json-rpc-provider": "4.1.8-preview-8b9e6f7b",
  "@metamask-previews/gas-fee-controller": "23.0.0-preview-8b9e6f7b",
  "@metamask-previews/json-rpc-engine": "10.0.3-preview-8b9e6f7b",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.7-preview-8b9e6f7b",
  "@metamask-previews/keyring-controller": "21.0.5-preview-8b9e6f7b",
  "@metamask-previews/logging-controller": "6.0.4-preview-8b9e6f7b",
  "@metamask-previews/message-manager": "12.0.1-preview-8b9e6f7b",
  "@metamask-previews/multichain": "4.0.0-preview-8b9e6f7b",
  "@metamask-previews/multichain-api-middleware": "0.2.0-preview-8b9e6f7b",
  "@metamask-previews/multichain-network-controller": "0.5.1-preview-8b9e6f7b",
  "@metamask-previews/multichain-transactions-controller": "0.9.0-preview-8b9e6f7b",
  "@metamask-previews/name-controller": "8.0.3-preview-8b9e6f7b",
  "@metamask-previews/network-controller": "23.3.0-preview-8b9e6f7b",
  "@metamask-previews/notification-services-controller": "6.0.0-preview-8b9e6f7b",
  "@metamask-previews/permission-controller": "11.0.6-preview-8b9e6f7b",
  "@metamask-previews/permission-log-controller": "3.0.3-preview-8b9e6f7b",
  "@metamask-previews/phishing-controller": "12.5.0-preview-8b9e6f7b",
  "@metamask-previews/polling-controller": "13.0.0-preview-8b9e6f7b",
  "@metamask-previews/preferences-controller": "17.0.0-preview-8b9e6f7b",
  "@metamask-previews/profile-sync-controller": "12.0.0-preview-8b9e6f7b",
  "@metamask-previews/queued-request-controller": "10.0.0-preview-8b9e6f7b",
  "@metamask-previews/rate-limit-controller": "6.0.3-preview-8b9e6f7b",
  "@metamask-previews/remote-feature-flag-controller": "1.6.0-preview-8b9e6f7b",
  "@metamask-previews/sample-controllers": "0.1.0-preview-8b9e6f7b",
  "@metamask-previews/selected-network-controller": "22.0.0-preview-8b9e6f7b",
  "@metamask-previews/signature-controller": "27.1.0-preview-8b9e6f7b",
  "@metamask-previews/token-search-discovery-controller": "3.1.0-preview-8b9e6f7b",
  "@metamask-previews/transaction-controller": "54.4.0-preview-8b9e6f7b",
  "@metamask-previews/user-operation-controller": "33.0.0-preview-8b9e6f7b"
}

@vinistevam vinistevam marked this pull request as ready for review May 8, 2025 13:23
@vinistevam vinistevam requested review from a team as code owners May 8, 2025 13:23
@@ -332,7 +337,8 @@ async function getNestedTransactionMeta(
async function addTransactionBatchWithHook(
request: AddTransactionBatchRequest,
): Promise<TransactionBatchResult> {
const { publishBatchHook, request: userRequest } = request;
const { publishBatchHook: initialPublishBatchHook, request: userRequest } =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, would this be more explicit as requestPublishBatchHook?

TransactionReceipt,
} from '../types';

const TRANSACTION_CHECK_INTERVAL = 5000; // 5 seconds
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this would also use the dynamic intervals from LaunchDarkly, so maybe it is cleaner to have a callback such as getPendingTransactionTrackerByChainId and then ask a tracker instance directly to poll our transaction?


const log = createModuleLogger(projectLogger, 'sequential-publish-batch-hook');

type SequentialPublishBatchHookParams = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, maybe SequentialPublishBatchHookOptions?

* @returns The hook function.
*/
getHook() {
return async ({
Copy link
Member

@matthewwalsh0 matthewwalsh0 May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than a function returning another function, should we use the pattern of having a normal #hook method that we bind here to keep it as readable as possible?

for (const transaction of transactions) {
try {
const transactionMeta = this.#getTransaction(String(transaction.id));
const transactionHash = await this.#publishTransaction(
Copy link
Member

@matthewwalsh0 matthewwalsh0 May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the logic is so small here, I wonder if it's simpler for the hook to directly call sendRawTransaction via the EthQuery?

): Promise<boolean> {
let attempts = 0;

while (attempts < MAX_TRANSACTION_CHECK_ATTEMPTS) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it out of our scope to define a timeout here, as it's so variable based on chain, and gas fees etc?

}

const waitPromise = new Promise((resolve) =>
setTimeout(resolve, TRANSACTION_CHECK_INTERVAL),
Copy link
Member

@matthewwalsh0 matthewwalsh0 May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than iterating and waiting ourselves, could we add methods to the PendingTransactionTracker such as addTransaction to force a new ID into the queue? And then maybe emit an event we wait for here inside a new Promise?

The goal being to encapsulate as much as possible in the PendingTransactionTracker and just add a job to it from here.


if (!isConfirmed) {
throw new Error(
`Transaction ${transactionHash} failed or was not confirmed.`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe Batch transaction failed - ${transactionHash} ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants